package com.xiaochun.jiankangapp.activity

import android.os.Bundle
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.xiaochun.jiankangapp.base.AppContext
import com.xiaochun.jiankangapp.base.BaseActivity
import com.xiaochun.jiankangapp.databinding.ActivityMainBinding
import com.xiaochun.jiankangapp.extension.onClick
import com.xiaochun.jiankangapp.extension.start
import com.xiaochun.jiankangapp.model.PersonalInfoModel
import com.xiaochun.jiankangapp.network.Request
import com.xiaochun.jiankangapp.utils.SPUtils
import kotlinx.coroutines.launch

class MainActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val viewBinding = setContentView<ActivityMainBinding>()
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.RESUMED) {
                loginViewModel.refreshPersonInfo()
            }
        }
        viewBinding.quit.onClick {
            loginViewModel.logout()
            start(LoginActivity::class.java)
            finish()
        }
        viewBinding.menu1.onClick {
            lifecycleScope.launch {
                showLoading()
                val r = Request.postSuspend<PersonalInfoModel>("api/material/details")
                if (r.isSuccessful && r.data != null) {
                    start(ResultActivity::class.java)
                } else {
                    start(InputInfoActivity::class.java)
                }
                hideLoading()
            }
        }
        viewBinding.menu2.onClick {
            start(BodyInfoActivity::class.java)
        }
        viewBinding.menu3.onClick {
            start(SendReportActivity::class.java)
        }
    }
}